

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>


  <title>sabinos.Timer | Components | Sabino's Net Project</title>

 <style type="text/css">
 
 code {white-space:pre;}
 
 </style>
  <script type="text/javascript" src="../prototype/prototype-1.6.0.3.js"></script>

  <script type="text/javascript" src="../source/sabinos.Timer.js"></script>

  <script language="javascript" type="text/javascript">
  
  //Function called after buton click
  function example1() {
      //Creates sabinos.Timer object that calls 'example1Function' function 
      // '1' second after start (wich is the default interval).
      var timer = new sabinos.Timer({Elapsed:example1Log});
      //Start timer object
      timer.start();
      //Clear console textbox
      $('textboxExample1').value='';
  }

  //Function called after Example 1 timer elapsed
  function example1Log(){
      $('textboxExample1').value='timer elapsed for Example 1\n';
  }
  
  //Creates sabinos.Timer object pointing to 'example2Elapsed' functon 
  var timer2 = new sabinos.Timer({
      AutoReset: true,                //Reset timer after each loop
      Interval: 2,                    //each loop takes 2 secunds to be fired after start/reset
      Loops: 5,                      //Reset will occur 10 times (set to -1 to repeat forever)
      Started: example2Started,       //'example2Started' function called each time timer2 starts
      Stoped: example2Stoped,         //'example2Stoped' function called each time timer2 stops
      Reseted: example2Reseted,        //'example2Reseted' function called each time timer2 resets
      Elapsed: example2Elapsed        //'example2Elapsed' function called each time timer2 elapses
      });
  
  //Function called after button start is clicked
  function example2Start() {
      //Start timer object
      timer2.start();
  }
  //Function called after button stop is clicked
  function example2Stop(){
      if(timer2!=null)
          timer2.stop();
  }
  function example2EnableChange(checkboxElement){
      if(timer2!=null)
          timer2.IsEnabled = $(checkboxElement).checked;
      else
          $(checkboxElement).checked = !$(checkboxElement).checked;
  }
  //function called each time timer2 elapses  
  function example2Elapsed() {
      $('textboxExample2').value+='Example2 Timer Elapsed\n';
  }
  //function called each time timer2 restes
  function example2Reseted() {
      $('textboxExample2').value+='Example2 Timer Reseted\n';
  }
  //function called each time timer2 starts
  function example2Started() {
      $('textboxExample2').value='Example2 Timer Started\n';
  }
  //function called each time timer2 stops
  function example2Stoped() {
      $('textboxExample2').value+='Example2 Timer Stoped\n';
  }
  //Function called after button Update info is clicked
  function example2GetInfo(){
      var timer2ElapsedSeconds = timer2.getElapsed();         //get amount of seconds elapsed since start action was performed
      var timer2LoopsCompleted = timer2.LoopsCompleted;       //get amount of loops completed since start action was performed 
      var timer2IsRunning = timer2.IsRunning;                 //get a bool value indicating if the timer is still runnning
      var timer2IsEnabled = timer2.IsEnabled;                 //get a bool value indicating if the timer is enabled
      
      var textBoxExample2 = $('textboxExample2');
      
      textBoxExample2.value='Example 2 timer info.\n';
      textBoxExample2.value+='getElapsed(): '+ timer2ElapsedSeconds + '.\n';
      textBoxExample2.value+='LoopsCompleted: ' + timer2LoopsCompleted + '.\n';
      textBoxExample2.value+='IsRunning: ' + timer2IsRunning + '.\n';
      textBoxExample2.value+='IsEnabled: ' + timer2IsEnabled + '.\n';
  }


  </script>

</head>


<body>


	<h1>Components -> sabinos.Timer</h1>
	<ul>
		<li><a href="#introduction"><i>Introduction</i></a></li>
		<li><a href="#examples"><i>Examples</i></a></li>
		<li><a href="#documentation"><i>Documentation</i></a></li>
		<li><a href="#download"><i>Download</i></a></li>
	</ul>
    <h1>
        <a name="introduction"></a>Introduction</h1>
    <p>
        sabinos.Timer is a javascript timer class implementation based on Prototype framework.</p>
    <h1>
        <a name="examples"></a>Examples</h1>
    <p>
        This is the simplest sabinos.Timer example. It shows a function being called after
        the timer elapses.</p>
    <p>
        <input id="buttonExample1" type="button" value="Start" class="button" onclick="example1();" />
    </p>
    <p>
        Console:<br />
        <textarea id="textboxExample1" rows="2" cols="70"></textarea>
    </p>
    <h2>
        HTML</h2>
    <code>&lt;input id="buttonExample1" type="button" value="Start" onclick="example1();" /&gt;
&lt;textarea id="textboxExample1"  rows="2" cols="70"&gt;&lt;/textarea&gt;
    </code>
    <h2>
        JavaScript</h2>
    <code>//Function called after buton click
function example1() {
    //Creates sabinos.Timer object that calls 'example1Function' function 
    // '1' second after start (wich is the default interval).
    var timer = new sabinos.Timer({Elapsed:example1Log});
    //Start timer object
    timer.start();
    //Clear console textbox
    $('textboxExample1').value='';
}

//Function called after Example 1 timer elapsed
function example1Log(){
    $('textboxExample1').value='timer elapsed for Example 1\n';
}
</code>
    <p>
        The next example is a little more complex and show the usage of more functions of
        the sabinos.Timer class, such as stoping it at any time, getting how much time was
        elapsed after the start button is pressed, getting if the timer is running or not,
        setting it to reset automaticaly each time it is elapsed and limiting the number
        of times it will be automatically reseted.<br />
        This example also shows the usage of others events besides the 'Elapsed' showned
        at Example 1.
    </p>
    <p>
        <input id="buttonExample2Start" type="button" class="button" value="Start" onclick="example2Start();" />&nbsp;
        <input id="buttonExample2Stop" type="button" class="button" value="Stop" onclick="example2Stop();" />
        <input type="checkbox" checked="checked"id="checkboxExample2" onclick="example2EnableChange(this)" />
		<label for="checkboxExample2">Timer enabled</label>
        <input id="buttonExample2Log" class="button" type="button" value="Get Timer Info" onclick="example2GetInfo();" />
    </p>
    <p>
        Console:<br />
        <textarea id="textboxExample2" rows="4" cols="70"></textarea>
    </p>
    <h2>
        HTML</h2>
    <code>
&lt;input id="buttonExample2Start" type="button" value="Start" onclick="example2Start();" /&gt;&nbsp;
&lt;input id="buttonExample2Stop" type="button" value="Stop" onclick="example2Stop();" /&gt;
&lt;input type="checkbox" checked="checked" id="checkboxExample2" onclick="example2EnableChange(this)" /&gt;
&lt;label for="checkboxExample2"&gt;Timer enabled&lt;/label&gt;
&lt;input id="buttonExample2Log" type="button" value="Get Timer Info" onclick="example2GetInfo();" /&gt;
    </code>
    <h2>
        JavaScript</h2>
    <code>    
//Creates sabinos.Timer object pointing to 'example2Elapsed' functon 
var timer2 = new sabinos.Timer({
    AutoReset: true,                //Reset timer after each loop
    Interval: 2,                    //each loop takes 2 secunds to be fired after start/reset
    Loops: 5,                      //Reset will occur 10 times (set to -1 to repeat forever)
    Started: example2Started,       //'example2Started' function called each time timer2 starts
    Stoped: example2Stoped,         //'example2Stoped' function called each time timer2 stops
    Reseted: example2Reseted,        //'example2Reseted' function called each time timer2 resets
    Elapsed: example2Elapsed        //'example2Elapsed' function called each time timer2 elapses
    });

//Function called after button start is clicked
function example2Start() {
    //Start timer object
    timer2.start();
}
//Function called after button stop is clicked
function example2Stop(){
    if(timer2!=null)
        timer2.stop();
}
function example2EnableChange(checkboxElement){
    if(timer2!=null)
        timer2.IsEnabled = $(checkboxElement).checked;
    else
        $(checkboxElement).checked = !$(checkboxElement).checked;
}
//function called each time timer2 elapses  
function example2Elapsed() {
    $('textboxExample2').value+='Example2 Timer Elapsed\n';
}
//function called each time timer2 restes
function example2Reseted() {
    $('textboxExample2').value+='Example2 Timer Reseted\n';
}
//function called each time timer2 starts
function example2Started() {
    $('textboxExample2').value='Example2 Timer Started\n';
}
//function called each time timer2 stops
function example2Stoped() {
    $('textboxExample2').value+='Example2 Timer Stoped\n';
}
//Function called after button Update info is clicked
function example2GetInfo(){
    var timer2ElapsedSeconds = timer2.getElapsed();         //get amount of seconds elapsed since start action was performed
    var timer2LoopsCompleted = timer2.LoopsCompleted;       //get amount of loops completed since start action was performed 
    var timer2IsRunning = timer2.IsRunning;                 //get a bool value indicating if the timer is still runnning
    var timer2IsEnabled = timer2.IsEnabled;                 //get a bool value indicating if the timer is enabled
    
    var textBoxExample2 = $('textboxExample2');
    
    textBoxExample2.value='Example 2 timer info.\n';
    textBoxExample2.value+='getElapsed(): '+ timer2ElapsedSeconds + '.\n';
    textBoxExample2.value+='LoopsCompleted: ' + timer2LoopsCompleted + '.\n';
    textBoxExample2.value+='IsRunning: ' + timer2IsRunning + '.\n';
    textBoxExample2.value+='IsEnabled: ' + timer2IsEnabled + '.\n';
}
    </code>
    <h1>
        <a name="documentation"></a>Documentation</h1>
    <p>
        To use sabinos.Timer you must instanciate the timer class that will be able to call functions based on times intervals</p>
    <h2>
        Instance</h2>
    <table align="center" width="90%" cellpadding="5" cellspacing="0" border="0">
	<thead>
        <tr>
            <th>
                Return</th>
            <th>
                Default</th>
            <th>
                Name</th>
            <th>
                Description</th>
        </tr>
		</thead>
		<tbody>
        <tr>
            <td>
                sabinos.Timer</td>
            <td>
                -</td>
            <td>
                initialize([Hash options])</td>
            <td>
                Creates an instance of sabinos.Timer</td>
        </tr>
		</tbody>
    </table>
    <h2>
        Options</h2>
    <table align="center" width="90%" cellpadding="5" cellspacing="0" border="0">
        <thead>
		<tr>
            <th>
                Return</th>
            <th>
                Default</th>
            <th>
                Name</th>
            <th>
                Description</th>
        </tr>
		</thead>
		<tbody>
        <tr>
            <td>
                number</td>
            <td>
                1</td>
            <td>
                Interval</td>
            <td>
                Time, in seconds, before the Elapsed event is called relative to the last occurrence
                of the Elapsed event</td>
        </tr>
        <tr class="dark">
            <td>
                boolean</td>
            <td>
                false</td>
            <td>
                AutoReset</td>
            <td>
                Indicates if the timer should reset after Elapsed event occur</td>
        </tr>
        <tr>
            <td>
                number</td>
            <td>
                -1</td>
            <td>
                Loops</td>
            <td>
                Amount of times that the timer resets if AutoReset is true. -1 value indicates that
                timer will repeat forever</td>
        </tr>
        <tr class="dark">
            <td>
                function</td>
            <td>
                Prototype.emptyFunction</td>
            <td>
                Elapsed</td>
            <td>
                Function called when the specified timer interval has elapsed and if the timer is Enabled
            </td>
        </tr>
        <tr>
            <td>
                function</td>
            <td>
                Prototype.emptyFunction</td>
            <td>
                Started</td>
            <td>
                Function called after the timer is started and if the timer is Enabled</td>
        </tr>
        <tr class="dark">
            <td>
                function</td>
            <td>
                Prototype.emptyFunction</td>
            <td>
                Stoped</td>
            <td>
                Function called after the timer is stoped and if the timer is Enabled</td>
        </tr>
        <tr>
            <td>
                function</td>
            <td>
                Prototype.emptyFunction</td>
            <td>
                Reseted</td>
            <td>
                Function called after the timer is reseted and if the timer is Enabled</td>
        </tr>
		</tbody>
    </table>
    <h2>Properties</h2>
        <table align="center" width="90%" cellpadding="5" cellspacing="0" border="0">
        <thead>
		<tr>
            <th>
                Return</th>
            <th>
                Default</th>
            <th>
                Name</th>
            <th>
                Description</th>
        </tr>
		</thead>
		<tbody>
        <tr>
            <td>
                number</td>
            <td>
                0</td>
            <td>
                LoopsCompleted</td>
            <td>
                Amount of times the timer was automaticaly reseted</td>
        </tr>
            <tr class="dark">
                <td>
                    boolean</td>
                <td>
                    false</td>
                <td>
                    IsRunning</td>
                <td>
                    Indicates if the timer is running or not</td>
            </tr>
            <tr>
                <td>
                    boolean</td>
                <td>
                    true</td>
                <td>
                    IsEnabled</td>
                <td>
                    Indicates if the timer is enabled or not. If timer is not enabled and AutoReset is set
                    to true the timer will continue to reset each time the time interval is elapsed
                    but yet no event will be fired and Properties will remain the same. Otherwise if
                    timer is not enabled and AutoReset is set to false no events will be fired after
                    the timer interval is elapsed and timer will not be reset again.</td>
            </tr>
			</tbody>
		</table>
    <h2>
        Methods</h2>
    <table align="center" width="90%" cellpadding="5" cellspacing="0" border="0">
        <thead>
		<tr>
            <th>
                Return</th>
            <th>
                Function</th>
            <th>
                Description</th>
        </tr>
		</thead>
		<tbody>
        <tr>
            <td>
                -</td>
            <td>
                start([bool resetLoopsCompleted, bool resetElapsedTime])
            </td>
            <td>
                Starts the timer with optional arguments to reset the number of loops and reset
                the elapsed time. Defaul behaviour:
                start(true, true)
            </td>
        </tr>
        <tr  class="dark">
            <td>
                -</td>
            <td>
                stop()</td>
            <td>
                Stops the timer.</td>
        </tr>
        <tr>
            <td>
                -</td>
            <td>
                reset()</td>
            <td>
                Rests the timer.</td>
        </tr>
        <tr class="dark">
            <td>
                number</td>
            <td>
                getElapsed()</td>
            <td>
                Gets the number os seconds elapsed since the last time timer was started.</td>
        </tr>
		</tbody>
    </table>
	<h1><a name="download"></a>Download</h1>
	<p>
		You can find all important files for this and other controls at the <a href="http://www.sabinos.net/site/download.html" title="Downloads">Download</a> section.
	</p>
	<!-- END CONTENT -->
    
	


</div>
</body>

</html>